package mybatis.test;

import mybatis.dao.SubjectDao;
import mybatis.pojo.Subject;
import mybatis.util.MybatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 * @Author 汤舒青
 * @Version 1.0
 */


public class TestSubject {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        try {
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
            //SQLsession ==Connection
            SqlSession sqlSession = build.openSession(true);
            //>>>Connection connection = sqlSession.getConnection();
            SubjectDao mapper = sqlSession.getMapper(SubjectDao.class);
            System.out.println(mapper.getClass().getName());
            System.out.println(Arrays.toString(mapper.getClass().getInterfaces()));
            List<Subject> all = mapper.findALL();
            all.forEach(System.out::println);
            //Subject subject = new Subject("养人妖", 5, 6);
//            int add = mapper.add(subject);
//            System.out.println(add);
            System.out.println(">>>>>>>>>>>>>");
            int findtotal = mapper.findtotal();
            System.out.println("课程总数" + findtotal);
//            int i = mapper.update(new Subject(1, "导弹维修", 20, 2));
//            System.out.println(i);
            System.out.println(">>>>>>>>>>>>>>>>>>>>");
//            System.out.println("通过id找单个对象");
//            Subject findsubject = mapper.findsubject(1);
//            System.out.println(findsubject);
//            System.out.println("删除课程");
//            int delate = mapper.delate(69);
//            System.out.println(delate==0?"删除失败":"删除成功");
//            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            System.out.println("分页查询");
            System.out.println("请输入第几页");
            int pageNo = input.nextInt();
            System.out.println("请输入一页显示多少");
            int pagesize = input.nextInt();
            HashMap<String, Integer> map = new HashMap<>();
            map.put("pageNo", (pageNo - 1) * pagesize);
            map.put("pagesize", pagesize);
            List<Subject> allByLimit = mapper.findAllByLimit(map);
            allByLimit.forEach(System.out::println);


            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 增
     */
    @Test
    public void add() {
        SqlSession s = MybatisUtil.get();
        SubjectDao mapper = s.getMapper(SubjectDao.class);
        Subject subject = new Subject("养人妖", 5, 6);
        int add = mapper.add(subject);
        System.out.println(add);
        s.commit();
        MybatisUtil.close(s);
    }

    /**
     * 删
     */
    @Test
    public void delate() {
        SqlSession s = MybatisUtil.get();
        SubjectDao mapper = s.getMapper(SubjectDao.class);
        System.out.println("删除课程");
        int delate = mapper.delate(70);
        System.out.println(delate == 0 ? "删除失败" : "删除成功");
        MybatisUtil.close(s);
    }

    /**
     * 改
     */
    @Test
    public void update() {

        SqlSession s = MybatisUtil.get();
        SubjectDao mapper = s.getMapper(SubjectDao.class);
        int i = mapper.update(new Subject(1, "导弹维修", 20, 2));
        System.out.println(i);
        MybatisUtil.close(s);

    }
    /**
     * 查
     */
    @Test
    public void find(){
        SqlSession s = MybatisUtil.get();
        SubjectDao mapper = s.getMapper(SubjectDao.class);
        System.out.println("通过id找单个对象");
        Subject findsubject = mapper.findsubject(1);
        System.out.println(findsubject);
    }

    /**
     * 分页
     */
    @Test
    public void findPage(){
        Scanner input = new Scanner(System.in);
        SqlSession s = MybatisUtil.get();
        SubjectDao mapper = s.getMapper(SubjectDao.class);
        System.out.println("分页查询");
        System.out.println("请输入第几页");
        int pageNo = input.nextInt();
        System.out.println("请输入一页显示多少");
        int pagesize = input.nextInt();
        HashMap<String, Integer> map = new HashMap<>();
        map.put("pageNo", (pageNo - 1) * pagesize);
        map.put("pagesize", pagesize);
        List<Subject> allByLimit = mapper.findAllByLimit(map);
        allByLimit.forEach(System.out::println);
    }
}
